﻿
using System;
using System.Diagnostics;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Threading;
using System.Runtime.Remoting.Channels.Ipc;

namespace Toolbox.AppInstanceManager
{

    public static class ApplicationInstanceManager
    {

        public static bool CreateSingleInstance(string name, EventHandler<InstanceCallbackEventArgs> callback)
        {

            EventWaitHandle eventWaitHandle = null;

            string eventName = string.Format("{0}-{1}", Environment.MachineName, name);

            InstanceProxy.IsFirstInstance = false;
            InstanceProxy.Sender = Process.GetCurrentProcess().MainModule.FileName;
            InstanceProxy.CommandLineArgs = Environment.GetCommandLineArgs();

            try
            {
                eventWaitHandle = EventWaitHandle.OpenExisting(eventName);
            }
            catch
            {
                InstanceProxy.IsFirstInstance = true;
            }


            if (InstanceProxy.IsFirstInstance)
            {
                eventWaitHandle = new EventWaitHandle(false, EventResetMode.AutoReset, eventName);

                ThreadPool.RegisterWaitForSingleObject(eventWaitHandle, WaitOrTimerCallback, callback, Timeout.Infinite, false);

                eventWaitHandle.Close();

                RegisterRemoteType(name);
            }
            else
            {
                UpdateRemoteObject(name);

                if (eventWaitHandle != null)
                {
                    eventWaitHandle.Set();
                }
            }

            return InstanceProxy.IsFirstInstance;
        }

        private static void UpdateRemoteObject(string uri)
        {

            IpcClientChannel clientChannel = new IpcClientChannel();

            ChannelServices.RegisterChannel(clientChannel, true);

            InstanceProxy proxy = Activator.GetObject(typeof(InstanceProxy), string.Format("ipc://{0}{1}/{1}", Environment.MachineName, uri)) as InstanceProxy;

            if (proxy is InstanceProxy)
            {
                proxy.SetCommandLineArgs(InstanceProxy.IsFirstInstance, InstanceProxy.Sender, InstanceProxy.CommandLineArgs);
            }

            ChannelServices.UnregisterChannel(clientChannel);
        }

        private static void RegisterRemoteType(string uri)
        {

            IpcServerChannel serverChannel = new IpcServerChannel(Environment.MachineName + uri);

            ChannelServices.RegisterChannel(serverChannel, true);

            RemotingConfiguration.RegisterWellKnownServiceType(typeof(InstanceProxy), uri, WellKnownObjectMode.Singleton);

            Process process = Process.GetCurrentProcess();
            process.Exited += delegate { ChannelServices.UnregisterChannel(serverChannel); };

        }

        private static void WaitOrTimerCallback(object state, bool timedOut)
        {
            EventHandler<InstanceCallbackEventArgs> callback = state as EventHandler<InstanceCallbackEventArgs>;
            if (callback is EventHandler<InstanceCallbackEventArgs>)
            {
                callback(state, new InstanceCallbackEventArgs(InstanceProxy.IsFirstInstance, InstanceProxy.CommandLineArgs));
            }
        }

    }
}
